<?php

/**
 * User: rongxia
 * Date: 2018/09/5
 * Time: 10:04
 * Description:
 * /**
 * 自动修复策略：
 *  定时重新跳到1个小时前去拉取数据，上两个小时都没有注单则不重新拉取
 */

namespace App\Console\Commands\ThirdOrder;


use App\Services\ThirdAPI\GameApiService;
use App\Services\ThirdAPI\CQ9APIService;
use App\Services\UserService;
use common\utils\RedisKeys;
use Illuminate\Support\Facades\Redis;


class CQ9OrderProcessor extends BaseOrderProcessor
{
    protected $signature = self::PREFIX . 'cq9 {--type=}';
    protected $description = 'CQ9注单拉取';
    public $gameNames;
    public $cqService;
    // 拉取注单的区间长度，比如从 4:00 - 5:00 一个小时的长度，根据注单量调整
    public $timeInterval = (60 * 10);

    // 当前最新的拉取注单更新时间
    public $latestUpdateKey;

    public $lockRedisKey = RedisKeys::CQ9_LOCK_AUTOFIX;
    public $updateRedisKey = RedisKeys::CQ9_UPDATE_TIME;

    /**
     * 初始化信息，子类需要重写这个方法
     */
    public function init($argv = null)
    {
        $this->cqService = new CQ9APIService();
        $this->partnerName = "CQ9";
        $info = GameApiService::getApiPartnerInfoByName($this->partnerName);
        $this->partnerID = $info['id'];
        $this->orderType = self::ORDER_TYPE_EGAME;
        $this->reportOrderType = self::GAME_TYPE_EGAME;
        $this->gameNames = [];
        $this->userInfos = [];
    }

    /**
     * 向第三方API抓取数据
     */
    public function catchData()
    {
        $startTime = OrderStrategy::getUpdateKey($this->lockRedisKey, $this->updateRedisKey);
        $startTime = strtotime($startTime);
        // 拉取 $this->timeInterval 的数据，减3秒是防止出现临界数据
        $this->latestUpdateKey = $endTime = min(time(), $startTime + $this->timeInterval - 3);
        $data = $this->cqService->getCQ9Orders($startTime, $endTime);
        return $data;
    }

    /**
     * 处理数据
     * 不可以放复杂操作，因为拉取到的每个数据都会经过 load 加载
     */
    public function load($data)
    {
        $this->updateTime = $data['update_time'];
        $this->betTime = $data['bet_time'];
        $this->settleTime = $data['settle_time'];
        $this->reportTime = $data['report_time'];
        $this->betMoney = $data['bet_money'];
        $this->prizeMoney = $data['prize_money'];
        $this->winLossMoney = $data['winloss_money'];
        $this->apiUserName = $data['api_username'];
        $this->contributeMoney = $data['contribute_money'];
        $this->bonusMoney = $data['bonus_money'];
        $this->validMoney = $data['valid_money'];
        $this->commonStatus = $data['common_status'];
        $this->updateKey = $data['update_key'];
        $this->apiStatus = $data['api_status'];
        $this->gameCode = $data['game_code'];
        $this->gameName = $data['game_name'];
        $this->betContent = $data['bet_content'];
        $this->originData = $data['origin_data'];
        $this->orderNumber = $data["order_number"];
    }

    /**
     * 处理数据，每个子类需要根据第三方的单条数据内容来获取有用的信息
     */
    public function initFromData($data)
    {

    }

    /**
     * 修复数据内容，每个子类需要实现怎么修复数据内容
     */
    public function fixData($date)
    {

    }

    /*
     * 更新redis拉取注单ID
     */
    public function updateFlag($updateKey)
    {
        $redis = Redis::connection();
        $redis->set($this->updateRedisKey, date("Y-m-d H:i:s", $this->latestUpdateKey));
    }
}